﻿Imports System.Data
Imports System.Xml

Public Class QLSinhVien
    Dim dtProvider As New SinhVienDAO()
    Private Sub QLSinhVien_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        SinhVienDAO.GetData()
        LayDSLop()
        LayDSSV()
    End Sub

    Private Sub btnThem_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThem.Click
        Dim chuoiLoi As String = KiemTra()
        If (String.Compare(chuoiLoi, "", True) = 0) Then
            Dim bangSinhVien As DataTable = SinhVienDAO.LayCauTrucBang()
            Dim sv As DataRow = bangSinhVien.NewRow()
            sv("MaSinhVien") = txtMSSV.Text.Trim()
            sv("HoTen") = txtHoTen.Text.Trim()
            sv("GioiTinh") = txtGioiTinh.Text.Trim()
            sv("NgaySinh") = txtNgaySinh.Value
            sv("SoDienThoai") = txtSoDT.Text.Trim()
            Dim lop As DataRowView = cboLop.Items(cboLop.SelectedIndex)
            sv("MaLop") = lop("MaLop")

            bangSinhVien.Rows.Add(sv)
            SinhVienDAO.CapNhat(bangSinhVien)
            LayDSSV()
            MessageBox.Show("Thêm thành công", "Thong bao", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Else
            MessageBox.Show(chuoiLoi, "Thong bao", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        dgvDSSinhVien.Rows.Remove(dgvDSSinhVien.CurrentRow)
        Dim bangSinhVien As DataTable = dgvDSSinhVien.DataSource
        SinhVienDAO.CapNhat(bangSinhVien)
        LayDSSV()
        MessageBox.Show("Xóa thành công", "Thong bao", MessageBoxButtons.OK, MessageBoxIcon.Information)
    End Sub

    Private Sub btnSua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSua.Click
        Dim chuoiLoi As String = KiemTra()
        If (String.Compare(chuoiLoi, "", True) = 0) Then
            Dim bangSinhVien As DataTable = dgvDSSinhVien.DataSource
            Dim sv As DataRow = bangSinhVien.Rows(dgvDSSinhVien.CurrentRow.Index)
            sv.BeginEdit()
            sv("MaSinhVien") = txtMSSV.Text.Trim()
            sv("HoTen") = txtHoTen.Text.Trim()
            sv("GioiTinh") = txtGioiTinh.Text.Trim()
            sv("NgaySinh") = txtNgaySinh.Value
            sv("SoDienThoai") = txtSoDT.Text.Trim()
            Dim lop As DataRowView = cboLop.Items(cboLop.SelectedIndex)
            sv("MaLop") = lop("MaLop")
            sv.EndEdit()
            SinhVienDAO.CapNhat(bangSinhVien)
            LayDSSV()
            MessageBox.Show("Sửa thành công", "Thong bao", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Else
            MessageBox.Show(chuoiLoi, "Thong bao", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
    End Sub

    Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
        Dim chuoiLoi As String = KiemTra()
        If (String.Compare(chuoiLoi, "", True) = 0) Then
            Dim bangSinhVien As DataTable = SinhVienDAO.LayCauTrucBang()
            Dim sv As DataRow = bangSinhVien.NewRow()
            sv("MaSinhVien") = txtMSSV.Text.Trim()
            sv("HoTen") = txtHoTen.Text.Trim()
            sv("GioiTinh") = txtGioiTinh.Text.Trim()
            sv("NgaySinh") = txtNgaySinh.Value
            sv("SoDienThoai") = txtSoDT.Text.Trim()
            Dim lop As DataRowView = cboLop.Items(cboLop.SelectedIndex)
            sv("MaLop") = lop("MaLop")

            bangSinhVien.Rows.Add(sv)
            SinhVienDAO.CapNhat(bangSinhVien)
            LayDSSV()
            MessageBox.Show("Update thành công", "Thong bao", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Else
            MessageBox.Show(chuoiLoi, "Thong bao", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
    End Sub
    Private Sub btnThoat_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        If (MessageBox.Show("Bạn có chắc chắn muốn thoát hay không?", "Thoát khỏi chương trình", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes) Then
            Me.Close()
            Global.System.Windows.Forms.Application.Exit()
        End If
    End Sub
    Private Sub dgvDSSinhVien_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvDSSinhVien.CellContentClick
        Dim dong As DataGridViewRow = dgvDSSinhVien.CurrentRow
        If (dong Is Nothing) Then
            Exit Sub
        End If
        txtMSSV.Text = dong.Cells("MaSinhVien").Value.ToString()
        txtHoTen.Text = dong.Cells("HoTen").Value.ToString()
        txtGioiTinh.Text = dong.Cells("GioiTinh").Value.ToString()
        txtNgaySinh.Value = dong.Cells("NgaySinh").Value
        txtSoDT.Text = dong.Cells("SoDienThoai").Value.ToString()

        Dim viTri As Integer = 0
        For viTri = 0 To cboLop.Items.Count - 1
            Dim lop As DataRowView = cboLop.Items(viTri)
            If (dong.Cells("MaLop").Value.ToString() = lop(0).ToString()) Then
                Exit For
            End If
        Next
        If (viTri = cboLop.Items.Count - 1) Then
            viTri = -1
        End If
        cboLop.SelectedIndex = viTri
    End Sub
    Public Function KiemTra() As String
        Dim chuoiLoi As String = ""
        If (String.Compare(txtMSSV.Text.Trim(), "", True) = 0) Then
            chuoiLoi &= "MaSinhVien phải khác rỗng" & vbCrLf
        End If

        If (String.Compare(txtHoTen.Text.Trim(), "", True) = 0) Then
            chuoiLoi &= "Họ tên phải khác rỗng" & vbCrLf
        End If
        Dim tuoi As Integer = Today.Year - txtNgaySinh.Value.Year
        If (tuoi < 18) Then
            chuoiLoi &= "Tuổi sinh viên không dưới 18" & vbCrLf
        End If
        Return chuoiLoi
    End Function

    Public Sub LayDSLop()
        cboLop.DataSource = LopHocDAO.LayDSLopHoc()
        cboLop.DisplayMember = "TenLop"
    End Sub

    Public Sub LayDSSV()
        Dim bangSinhVien As DataTable = SinhVienDAO.LayDSSinhVien()
        dgvDSSinhVien.DataSource = bangSinhVien
        dgvDSSinhVien.Columns(0).Visible = True
        dgvDSSinhVien.Columns(1).Visible = True
        dgvDSSinhVien.Columns(2).Visible = True
        dgvDSSinhVien.Columns(3).DefaultCellStyle.Format = "dd/MM/yyyy"
        dgvDSSinhVien.Columns(5).Visible = False

        dgvDSSinhVien.Columns(1).HeaderText = "Họ tên"
        dgvDSSinhVien.Columns(2).HeaderText = "Giới tính"
        dgvDSSinhVien.Columns(3).HeaderText = "Ngày sinh"
        dgvDSSinhVien.Columns(4).HeaderText = "Số điện thoại"
        dgvDSSinhVien.Columns(6).HeaderText = "Tên lớp"

        For i As Integer = 0 To dgvDSSinhVien.Columns.Count - 1
            dgvDSSinhVien.Columns(i).ReadOnly = True
        Next
    End Sub
End Class